
CC := aarch64-linux-gnu-gcc
# CC32 := aarch64-linux-gnu-gcc
# CC := armclang --target=aarch64-arm-none-eabi -march=armv8-a
# CC32 := armclang --target=aarch64-arm-none-eabi -march=armv8-a
# CC32 := armclang --target=arm-arm-none-eabi -march=armv8-a
CC32 := armv8l-linux-gnueabihf-gcc 
# CC32 := arm-linux-gnueabi-gcc
LD32 := armv8l-linux-gnueabihf-ld

LD := aarch64-linux-gnu-ld
OBJDUMP := aarch64-linux-gnu-objdump
OBJCOPY := aarch64-linux-gnu-objcopy

TARGET := avk

ifeq ($(TARGET),hk)
CFLAGS := -DUART_ADDR=0x70000000
# CFLAGS := -DUART_ADDR=0x70001000
endif
ifeq ($(TARGET),avk)
CFLAGS := -DUART_ADDR=0x13000000
endif

A32_ENTRY := 0x80008000
MIX_A32_ASM_FILE := tmp/32-inst-64-data-mix.S
MIX_A32_OBJS := $(MIX_A32_ASM_FILE:%.S=%.o)


boot: boot.o vec_el3.o simple_print.o
	$(LD) -Tlink.lds $^ -o $(TARGET)/$@.elf
	$(OBJDUMP) -d $(TARGET)/$@.elf > $(TARGET)/$@.asm
	$(OBJCOPY) -O binary $(TARGET)/$@.elf  $(TARGET)/$@.bin

all: $(TARGET) boot_64_mix_32
	
boot_64_mix_32: boot.o $(MIX_A32_OBJS)
	$(LD) -Tlink-mix.lds $^ -o $(TARGET)/$@.elf
	$(OBJDUMP) -d $(TARGET)/$@.elf > $(TARGET)/$@.asm
	$(OBJCOPY) -O binary $(TARGET)/$@.elf  $(TARGET)/$@.bin
	@rm $(MIX_A32_ASM_FILE)

boot-a32: boot-a32.o mon_vec-a32.o hyp_vec-a32.o svc_vec-a32.o simple_print-a32.o
	$(LD32) -Tlink-a32.lds -o $(TARGET)/$@.elf $^
	$(OBJCOPY) -O binary $(TARGET)/$@.elf  $(TARGET)/$@.bin
	$(OBJDUMP) -d $(TARGET)/$@.elf > $(TARGET)/$@.asm
	@mkdir -p `dirname $(MIX_A32_ASM_FILE)`
	@echo -e ".global _a64_mix_a32_entry\n_a64_mix_a32_entry:" > $(MIX_A32_ASM_FILE)
	hexdump $(TARGET)/$@.bin -ve '1/4 "    .word 0x%08x\n"' >> $(MIX_A32_ASM_FILE)

$(TARGET):
	mkdir -p $(TARGET)

$(MIX_A32_ASM_FILE): boot-a32

%-a32.o: %-a32.S
	$(CC32) $(CFLAGS) -c -o $@ $^

%.o: %.S
	$(CC) $(CFLAGS) -c -o $@ $^


%-a32.o: %-a32.c
	$(CC32) $(CFLAGS) -c -o $@ $^

%.o: %.c
	$(CC) $(CFLAGS) -c -o $@ $^

clean:
	find -type f -name "*.o" -exec rm {} \;
	rm tmp -r